"""
存放的是后台商品添加页面操作,与相关元素的定位
"""
from Page.back_login_page import *
from time import sleep
import os

login_url = "http://localhost:8080/ecshop/admin/"


class AddGoodsPage(Back_login_page):
    goods_loc = ("class name", "icon-goods")  # 商品管理定位
    add_goods_loc = ("link text", "添加新商品")  # 添加新商品定位
    menu_frame = "menu-frame"  # 表单name
    main_frame = "main-frame"
    goods_name_loc = ("name", "goods_name")  # 商品名称定位
    name_style_loc = ("name", "goods_name_style")  # 名称字体下拉框定位
    sn_loc = ("name", "goods_sn")  # 商品编号定位
    cat_loc = ("name", "cat_id")  # 商品分类下拉框定位
    add_cat_loc = ("css selector", "button[onclick='rapidCatAdd()']")  # 添加分类定位
    addcat_loc = ("name", "addedCategoryName")  # 添加分类定位
    addcat_sure_loc = ("css selector", "button[onclick='addCategory()']")  # 确认添加分类按钮定位
    addcat_turn_loc = ("css selector", "button[onclick='return goCatPage()']")  # 分类管理按钮定位
    expand_cat_loc = ("css selector", "button[onclick='addOtherCat(this.parentNode)']")  # 拓展分类定位
    brand_loc = ("name", "brand_id")  # 商品品牌下拉框定位
    add_brand_loc = ("css selector", "button[onclick='rapidBrandAdd()']")  # 添加品牌定位
    addbrand_loc = ("name", "addedBrandName")  # 添加分类定位
    addbrand_sure_loc = ("css selector", "button[onclick='addBrand()']")  # 确定添加品牌定位
    addbrand_turn_loc = ("css selector", "button[onclick='return goBrandPage()']")  # 添加品牌定位
    suppliers_loc = ("name", "suppliers_id")  # 供货商下拉框定位
    price_loc = ("name", "shop_price")  # 本店价格定位
    price_set_loc = ("css selector", "input[onclick='marketPriceSetted()']")  # 按市场价按钮定位
    rank_price_1_loc = ("id", "rank_1")  # 会员价格定位
    rank_price_2_loc = ("id", "rank_2")
    rank_price_3_loc = ("id", "rank_3")
    market_price_loc = ("name", "market_price")  # 市场价格定位
    set_int_price_loc = ("css selector", "input[onclick='integral_market_price()']")  # 价格取整定位
    sale_num_loc = ("name", "virtual_sales")  # 销量定位
    integral_loc = ("name", "integral")  # 使用积分定位
    is_promote_loc = ("id", "is_promote")  # 促销定位
    promote_stm_loc = "selbtn1"  # 促销开始时间定位
    promote_etm_loc = "selbtn2"  # 促销结束时间定位
    promote_1_loc = ("id", 'promote_1')  # 促销价定位
    goods_img_loc = ("name", "goods_img")  # 商品图片定位
    goods_img_url_loc = ("name", "goods_img_url")  # 外部链接图片定位
    little_img_loc = ("id", "auto_thumb")  # 缩略图自动生成按钮
    goods_thumb_loc = ("name", "goods_thumb")  # 缩略图上传定位
    goods_thumb_url_loc = ("name", "goods_thumb_url")  # 缩略图外部链接上传定位
    sure_loc = ("css selector", "div.button-div input[value=' 确定 ']")  # 确定按钮定位
    reset_loc = ("css selector", "input[type='reset']")  # 重置按钮定位

    detailed_description_loc = ("id", "detail-tab")  # 定位 详细描述 元素
    iframe_loc = ("id", "goods_desc___Frame")  # 详细描述页一级表单定位
    iframe1_loc = ("css selector", "iframe[frameborder='0']")  # 详细描述页二级表单定位
    xxms_loc = ("css selector", "body")  # 详细描述输入框定位

    other_loc = ("id", "mix-tab")  # 其他信息定位
    goods_weight_loc = ("name", "goods_weight")  # 商品重量定位
    goods_number_loc = ("name", "goods_number")  # 商品库存定位
    warn_number_loc = ("name", "warn_number")  # 库存警告定位
    is_best_loc = ("name", "is_best")  # 精品定位
    is_new_loc = ("name", "is_new")  # 新品定位
    is_hot_loc = ("name", "is_hot")  # 热销定位
    is_on_sale_loc = ("name", "is_on_sale")  # 上架定位
    is_alone_sale_loc = ("name", "is_alone_sale")  # 单独销售定位
    is_shipping_loc = ("name", "is_shipping")  # 包邮定位
    keywords_loc = ("name", "keywords")  # 关键词定位
    goods_brief_loc = ("name", "goods_brief")  # 简单描述定位
    seller_note_loc = ("name", "seller_note")  # 商家备注定位

    properties_loc = ("id", "properties-tab")  # 商品属性定位
    goods_type_loc = ("name", "goods_type")  # 商品类型下拉框定位

    gallery_loc = ("id", "gallery-tab")  # 商品相册定位
    img_desc_loc = ("name", "img_desc[]")  # 图片描述
    img_url_loc = ("name", "img_url[]")  # 相册上传定位
    img_file_loc = ("name", "img_file[]")  # 外部相册链接定位

    def get_background(self, admin_username, admin_password):
        """
        进入商城后台
        :param admin_username: 后台用户名
        :param admin_password: 后台密码
        :return:
        """
        self.open_url(login_url)
        self.click_X()
        self.click_cloud_log()
        self.input_name(admin_username)
        self.input_password(admin_password)
        self.click_login()

    def get_add_goods(self):
        '''
        进入后台添加商品表单(包括点击切换表单)
        :return:
        '''
        self.switch_to_frame(self.menu_frame)
        self.click(self.goods_loc)
        sleep(10)
        self.click(self.add_goods_loc)
        self.driver.switch_to.parent_frame()
        self.switch_to_frame(self.main_frame)

    def currencyInfo_need(self, goods_name, cat, price):
        '''
        通用信息表单必要信息填写
        :param goods_name:商品名称
        :param cat: 商品分类
        :param price:商品价格
        :return:
        '''
        self.clear_send_text(self.goods_name_loc, goods_name)  # 商品名称定位输入
        self.select_by_text(self.cat_loc, cat)  # 选择商品分类(输入索引或选项文本)
        self.clear_send_text(self.price_loc, price)  # 输入本店价格

    def name_style(self, goods_name, name_style, cat, price):
        '''
        设置商品名称字体
        :param goods_name:商品名称
        :param name_style:名称字体
        :param cat:商品分类
        :param price:商品价格
        :return:
        '''
        self.currencyInfo_need(goods_name, cat, price)  # 输入必填项
        self.select_by_text(self.name_style_loc, name_style)  # 选择商品名称字体

    def goods_sn(self, goods_name, cat, price, goods_sn):
        self.currencyInfo_need(goods_name, cat, price)  # 输入必填项
        self.send_text(self.sn_loc, goods_sn)  # 输入商品编号

    def add_cat(self, catName):
        self.click(self.add_cat_loc)  # 点击添加分类
        self.send_text(self.addcat_loc, catName)  # 输入分类
        self.click(self.addcat_sure_loc)  # 点击确认添加分类

    def cat_turn(self):
        self.click(self.add_cat_loc)  # 点击添加分类
        self.click(self.addcat_turn_loc)  # 点击分类管理
        self.driver.switch_to.alert.accept()  # 点击弹窗确认页面跳转

    def add_brand(self, brand_name):
        self.click(self.add_brand_loc)  # 点击添加商品品牌
        self.send_text(self.addbrand_loc, brand_name)  # 输入商品品牌
        self.click(self.addbrand_sure_loc)  # 点击确定添加品牌

    def brand_turn(self):
        self.click(self.add_brand_loc)  # 点击添加分类
        self.click(self.addbrand_turn_loc)  # 点击分类管理
        self.driver.switch_to.alert.accept()  # 点击弹窗确认页面跳转

    def brand_choose(self, goods_name, cat, price, brand_name):
        self.currencyInfo_need(goods_name, cat, price)  # 输入必填项
        self.select_by_text(self.brand_loc, brand_name)  # 选择商品品牌(输入索引或选项文本)

    def suppliers_choose(self, goods_name, cat, price, suppliers):
        self.currencyInfo_need(goods_name, cat, price)  # 输入必填项
        self.select_by_text(self.suppliers_loc, suppliers)  # 选择供货商(输入索引或选项文本)

    def price_set(self, goods_name, cat, price, price_1, price_2, price_3):
        self.currencyInfo_need(goods_name, cat, price)  # 输入必填项
        self.clear_send_text(self.rank_price_1_loc, price_1)  # 输入会员价格
        self.clear_send_text(self.rank_price_2_loc, price_2)  # 输入供销商价格
        self.clear_send_text(self.rank_price_3_loc, price_3)  # 输入vip价格

    def market_price(self, goods_name, cat, price, market_price):
        self.currencyInfo_need(goods_name, cat, price)  # 输入必填项
        self.clear_send_text(self.market_price_loc, market_price)  # 输入市场价格

    def market_price_refl(self, goods_name, cat, price):
        self.currencyInfo_need(goods_name, cat, price)  # 输入必填项
        self.click(self.set_int_price_loc)  # 点击市场价取整

    def market_set_price(self, goods_name, cat, market_price):
        self.send_text(self.goods_name_loc, goods_name)  # 商品名称定位输入
        self.select_by_text(self.cat_loc, cat)  # 选择商品分类(输入索引或选项文本)
        self.send_text(self.market_price_loc, market_price)  # 输入市场价格
        self.click(self.price_set_loc)  # 点击按照市场价格确定本店价格

    def goods_num(self, goods_name, cat, price, sale_num):
        self.currencyInfo_need(goods_name, cat, price)  # 输入必填项
        self.clear_send_text(self.sale_num_loc, sale_num)  # 输入虚拟销量

    def click_promote(self):
        promote = self.find_element(self.is_promote_loc)
        if not promote.is_selected:
            promote.click()  # 点击促销
        return promote

    def get_promote_stm(self):
        return self.find_element(("id", self.promote_stm_loc))  # 获取促销开始日期元素

    def get_promote_etm(self):
        return self.find_element(("id", self.promote_etm_loc))  # 获取促销结束日期元素

    def promote_price(self, goods_name, cat, price, promote_price):
        self.currencyInfo_need(goods_name, cat, price)  # 输入必填项
        self.click_promote()
        self.clear_send_text(self.promote_1_loc, promote_price)  # 输入促销价格

    def set_promote_time(self, goods_name, cat, price, promote_price, time_start, time_end):
        self.currencyInfo_need(goods_name, cat, price)  # 输入必填项
        self.click_promote()
        self.clear_send_text(self.promote_1_loc, promote_price)  # 输入促销价格
        js1 = f"document.getElementById('selbtn1').setAttribute('value',{time_start})"
        self.driver.execute_script(js1)  # 设置促销开始日期
        js2 = f"document.getElementById('selbtn2').setAttribute('value',{time_end})"
        self.driver.execute_script(js2)  # 设置促销结束日期

    def goods_img_upload(self, goods_name, cat, price, imgPath):
        self.currencyInfo_need(goods_name, cat, price)  # 输入必填项
        if "/" in imgPath:
            self.send_text(self.goods_img_loc, imgPath)  # 上传图片
        else:
            path = os.path.dirname(__file__) + f"/testimg/{imgPath}"
            self.send_text(self.goods_img_url_loc, path)  # 上传图片

    def get_goods_thumb(self):
        goods_thumb = self.find_element(self.little_img_loc)
        if not goods_thumb.is_selected:
            goods_thumb.click()  # 点击自动生成缩略图
        return goods_thumb

    def get_thumb_up(self):
        goods_thumb_up = self.find_element(self.goods_thumb_loc)
        return goods_thumb_up  # 缩略图上传

    def get_thumb_urlup(self):
        goods_thumb_urlup = self.find_element(self.goods_thumb_url_loc)
        return goods_thumb_urlup  # 缩略图外部链接上传

    def goods_thumb_img_upload(self, goods_name, cat, price, imgPath):
        self.currencyInfo_need(goods_name, cat, price)  # 输入必填项
        goods_thumb = self.get_goods_thumb()
        if goods_thumb.is_selected:
            self.click(self.little_img_loc)  # 取消自动生成缩略图
        if "/" in imgPath:
            self.send_text(self.goods_thumb_url_loc, imgPath)  # 上传缩略图
        else:
            path = os.path.dirname(__file__) + f"/testimg/{imgPath}"
            self.send_text(self.goods_thumb_loc, path)  # 上传缩略图

    def minuteInfo(self, goods_name, cat, price, content=None):
        self.currencyInfo_need(goods_name, cat, price)  # 输入必填项
        # 定位 点击 详细描述 页面
        self.click(self.detailed_description_loc)
        # 切换表单
        self.switch_to_frame(self.iframe_loc)
        self.switch_to_frame(self.iframe1_loc)
        # 输入详细描述信息
        body = "这里是通过js发的正文内容"
        js = 'document.getElementById("Editor_Edit_EditorBody_ifr").contentWindow.document.body.innerHTML="%s"' % content
        driver.execute_script(js)
        self.send_text(self.xxms_loc, content)
        # 切换表单到默认层级
        self.switch_to_parent_frame()
        self.switch_to_parent_frame()

    def otherInfo(self, goods_name, cat, price):
        self.currencyInfo_need(goods_name, cat, price)  # 输入必填项
        self.click(self.other_loc)  # 点击其他信息

    def other_turnless(self, weight):
        self.send_text(self.goods_weight_loc, weight)

    def other_weight(self, goods_name, cat, price, weight):
        self.otherInfo(goods_name, cat, price)
        self.send_text(self.goods_weight_loc, weight)

    def goods_number(self, goods_name, cat, price, goods_number):
        self.otherInfo(goods_name, cat, price)  # 输入必填项
        self.clear_send_text(self.goods_number_loc, goods_number)

    def warn_number(self, goods_name, cat, price, warn_number):
        self.otherInfo(goods_name, cat, price)  # 输入必填项
        self.clear_send_text(self.warn_number_loc, warn_number)

    def is_best(self, goods_name, cat, price):
        self.otherInfo(goods_name, cat, price)  # 输入必填项
        is_best = self.find_element(self.is_best_loc)
        if not is_best.is_selected:
            is_best.click()

    def is_new(self, goods_name, cat, price):
        self.otherInfo(goods_name, cat, price)  # 输入必填项
        is_new = self.find_element(self.is_new_loc)
        if not is_new.is_selected:
            is_new.click()

    def is_hot(self, goods_name, cat, price):
        self.otherInfo(goods_name, cat, price)  # 输入必填项
        is_hot = self.find_element(self.is_hot_loc)
        if not is_hot.is_selected:
            is_hot.click()

    def is_on_sale(self, goods_name, cat, price):
        self.otherInfo(goods_name, cat, price)  # 输入必填项
        is_on_sale = self.find_element(self.is_on_sale_loc)
        if not is_on_sale.is_selected:
            is_on_sale.click()

    def is_shipping(self, goods_name, cat, price):
        self.otherInfo(goods_name, cat, price)  # 输入必填项
        is_shipping = self.find_element(self.is_shipping_loc)
        if not is_shipping.is_selected:
            is_shipping.click()

    def keywords(self, goods_name, cat, price, keywords):
        self.otherInfo(goods_name, cat, price)  # 输入必填项
        self.send_text(self.keywords_loc, keywords)  # 输入关键词

    def goods_brief(self, goods_name, cat, price, goods_brief):
        self.otherInfo(goods_name, cat, price)  # 输入必填项
        self.send_text(self.goods_brief_loc, goods_brief)   # 输入简单描述

    def seller_note(self, goods_name, cat, price, seller_note):
        self.otherInfo(goods_name, cat, price)  # 输入必填项
        self.send_text(self.seller_note_loc, seller_note)   # 输入商家备注

    def goods_attr(self, goods_name, cat, price):
        self.currencyInfo_need(goods_name, cat, price)  # 输入必填项
        self.click(self.properties_loc)

    def attr_turnless(self, goods_type):
        self.send_text(self.goods_type_loc, goods_type)

    def goods_type(self, goods_name, cat, price, goods_type):
        self.goods_attr(goods_name, cat, price)  # 输入必填项
        self.select_by_text(self.goods_type_loc, goods_type)    # 选择类型

    def goods_album(self, goods_name, cat, price):
        self.currencyInfo_need(goods_name, cat, price)  # 输入必填项
        self.click(self.gallery_loc)

    def album_turnless(self, img_desc):
        self.send_text(self.img_desc_loc, img_desc)

    def album_img_desc(self, goods_name, cat, price, img_desc):
        self.goods_album(goods_name, cat, price)  # 输入必填项
        self.send_text(self.img_desc_loc, img_desc)  # 输入相册描述

    def album_img_url(self, goods_name, cat, price, imgPath):
        self.goods_album(goods_name, cat, price)  # 输入必填项
        if "/" in imgPath:
            self.send_text(self.img_file_loc, imgPath)  # 上传相册
        else:
            path = os.path.dirname(__file__) + f"/testimg/{imgPath}"
            self.send_text(self.goods_img_url_loc, path)  # 上传相册

    def click_sure(self):
        self.click(self.sure_loc)  # 点击确定

    def click_reset(self, goods_name, cat, price):
        self.currencyInfo_need(goods_name, cat, price)  # 输入必填项
        self.click(self.reset_loc)  # 点击重置


if __name__ == '__main__':
    # 打开浏览器
    driver = open_browser()
    lg = AddGoodsPage(driver)
    lg.get_background("admin", "admin123")
    lg.get_add_goods()
    # lg.currencyInfo_need("自动化从入门到放弃", 1, "288")
    # lg.minuteInfo("默认为空")
